package com.fh.controller.building;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.service.building.RecordService;
import com.fh.util.Const;
import com.fh.util.PageData;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fh.controller.system.tools.GetDatalayui.getLData;
import static com.fh.controller.system.tools.GetDatalayui.getPData;

/**
 * Created by caozengling on 2017/10/23.
 */
@Controller
@RequestMapping(value = "/recordController")
public class operaRecord extends BaseController {
    @Resource(name = "recordService")
    private RecordService recordService;
    /**
     * 返回list 操作记录
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object json(Page page) {
        PageData pd = new PageData();
        Map<String, Object> map = new HashMap<String, Object>();
        List<PageData> varList;
        String datalayui = null;
        try {
            pd = this.getPageData();
            getPData(page, pd);
            varList = recordService.list(page);
            datalayui = getLData(page, varList);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            logAfter(logger);
        }
        return datalayui;
    }

    /**
     * 根据合同ID获取贷款合同附件
     */
    @RequestMapping(value = "/attlist")
    @ResponseBody
    public PageData loaninfo() {
        logBefore(logger, "根据合同ID获取房屋图片a");
        PageData res = new PageData();
        PageData pd = this.getPageData();
        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            PageData data = new PageData();
            pd.put("USER_ID", user.getUSER_ID());
            List<PageData> attachmentData = recordService.attlist(pd);
            if(attachmentData.size()>0){
                data.put("attachment", attachmentData);
            }
            res.put("data", data);
            res.put("errcode", "0");
            res.put("errmsg", "ok");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            res.put("data", "");
            res.put("errcode", "1");
            res.put("errmsg", e.toString());
        }

        return res;
    }

    /**
     * 返回房间弹出层的 操作记录
     */
    @RequestMapping(value = "/listroom")
    @ResponseBody
    public Object listroom(Page page) {
        PageData pd = new PageData();
        Map<String, Object> map = new HashMap<String, Object>();
        List<PageData> varList;
        String datalayui = null;
        try {
            pd = this.getPageData();
            getPData(page, pd);
            varList = recordService.listroom(page);
            datalayui = getLData(page, varList);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            logAfter(logger);
        }
        return datalayui;
    }
    /**
     * 返回全局操作 操作记录
     */
    @RequestMapping(value = "/Systemlog")
    @ResponseBody
    public Object Systemlog(Page page) {
        PageData pd = new PageData();
        Map<String, Object> map = new HashMap<String, Object>();
        List<PageData> varList;
        String datalayui = null;

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            pd = this.getPageData();
            pd.put("COMPANY_ID", user.getCOMPANY_ID());
            getPData(page, pd);
            varList = recordService.Systemlog(page);
            datalayui = getLData(page, varList);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            logAfter(logger);
        }
        return datalayui;
    }
    /**
     * 返回前台操作 操作记录
     */
    @RequestMapping(value = "/Systemlog_tb_record")
    @ResponseBody
    public Object Systemlog_tb_record(Page page) {
        PageData pd = new PageData();
        Map<String, Object> map = new HashMap<String, Object>();
        List<PageData> varList;
        String datalayui = null;

        //获取当前登录用户
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        User user = (User) session.getAttribute(Const.SESSION_USER);

        try {
            pd = this.getPageData();
            pd.put("COMPANY_ID", user.getCOMPANY_ID());
            getPData(page, pd);
            varList = recordService.Systemlog_tb_record(page);
            datalayui = getLData(page, varList);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            logAfter(logger);
        }
        return datalayui;
    }
}
